package cn.lili.controller.distribution;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.entity.dos.DistributionGoods;
import cn.lili.modules.distribution.entity.dto.DistributionGoodsSearchParams;
import cn.lili.modules.distribution.entity.vos.DistributionGoodsVO;
import cn.lili.modules.distribution.entity.vos.DistributionInfoVO;
import cn.lili.modules.distribution.service.DistributionGoodsService;
import cn.lili.modules.distribution.service.DistributionSelectedGoodsService;
import cn.lili.modules.goods.entity.dos.Category;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 分销商品管理接口
 *
 * @author pikachu
 * @since 2020-03-14 23:04:56
 */
@RestController
@Api(tags = "分销商品管理接口")
@RequestMapping("/distribution/goods")
@RequiredArgsConstructor
public class DistributionGoodsController {


    private final DistributionGoodsService distributionGoodsService;

    private final DistributionSelectedGoodsService distributionSelectedGoodsService;

    @GetMapping
    @ApiOperation(value = "分页获取")
    public ResultMessage<Page<DistributionGoodsVO>> getByPage(DistributionGoodsSearchParams distributionGoodsSearchParams) {
        return ResultUtil.data(distributionGoodsService.goodsPage(distributionGoodsSearchParams));
    }

    @ApiOperation(value = "选择分销商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "distributionGoodsId", value = "分销ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "checked", value = "是否选择", required = true, dataType = "boolean", paramType = "query")
    })
    @GetMapping(value = "/selected/{distributionGoodsId}")
    public ResultMessage<Object> selectDistributionGoods(
            @NotNull(message = "分销商品不能为空") @PathVariable("distributionGoodsId") String distributionGoodsId, Boolean checked) {
        boolean result;
        if (Boolean.TRUE.equals(checked)) {
            result = distributionSelectedGoodsService.add(distributionGoodsId);
        } else {
            result = distributionSelectedGoodsService.delete(distributionGoodsId);
        }
        //判断操作结果
        if (result) {
            return ResultUtil.success();
        } else {
            throw new ServiceException();
        }

    }

    @ApiOperation(value = "选择商品参与分销")
    @ApiImplicitParam(name = "skuId", value = "规格ID", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/checked")
    public ResultMessage<Void> distributionCheckGoods(@NotNull(message = "规格ID不能为空") String skuId) {
        distributionGoodsService.checked(skuId, null);
        return ResultUtil.success();
    }

    @ApiOperation(value = "取消分销商品")
    @ApiImplicitParam(name = "id", value = "分销商商品ID", required = true, paramType = "path")
    @DeleteMapping(value = "/cancel/{id}")
    public ResultMessage<Object> cancel(@NotNull @PathVariable String id) {

        //清除分销商品
        distributionGoodsService.cancel(id);

        return ResultUtil.success();
    }

    @DeleteMapping(value = "/delByIds/{ids}")
    @ApiOperation(value = "批量删除")
    public ResultMessage<Object> delAllByIds(@PathVariable List ids) {

        distributionGoodsService.removeByIds(ids);
        return ResultUtil.success();
    }

    /**
     * 根据openId查询是否是分销以及其橱窗商品
     */
    @ApiOperation("根据openId查询是否是分销以及其橱窗商品")
    @GetMapping("/getDistributionGoodByOpenId")
    public ResultMessage<DistributionInfoVO> getDistributionGoodByOpenId(DistributionGoodsSearchParams searchParams) {
        return ResultUtil.data(distributionGoodsService.getDistributionGoodByOpenId(searchParams));
    }

    @ApiOperation("根据openId查询是否是分销以及其橱窗商品数量")
    @GetMapping("/getDistributionGoodCountByOpenId")
    public ResultMessage<DistributionInfoVO> getDistributionGoodCountByOpenId(DistributionGoodsSearchParams searchParams) {
        return ResultUtil.data(distributionGoodsService.getDistributionGoodCountByOpenId(searchParams));
    }


    @ApiOperation("根据openid查询分销商品分类")
    @GetMapping("/getDistributionGoodCategroyListByOpenId")
    public ResultMessage<List<Category>> getDistributionGoodCategroyListByOpenId(@RequestParam("openId") String openId) {
        return ResultUtil.data(distributionGoodsService.getDistributionGoodCategroyListByOpenId(openId));
    }
}
